unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Timer2: TTimer;
    BitBtn1: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);

  private
    procedure GetEmColoured;
    procedure BitBtn1Click(Sender: TObject);
    procedure ChangeEm;
    procedure ChangeEmFinally;
  public
    { Public declarations }
  end;

const
  N=10;
  V=15;
  L=20;
  A=70;


var
  Form1: TForm1;
  pan: array of TPanel;
  flag: Boolean;
  num, smth: array of integer;

  i, j, m, t, lock: integer;


implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;


  setlength(smth, N);
  setlength(pan, N);
  setlength(num, N);

  for i:=0 to N-1 do
  begin

    num[i]:=random(60);
    smth[i]:=random(60);
  end;
  for i:=0 to N-1 do
  begin
     pan[i]:=TPanel.Create(Form1);
     pan[i].Parent:=Form1;
     pan[i].Width:=A;
     pan[i].Height:=A;
     pan[i].Top:=250;
     pan[i].Left:=170+(A+L)*i;
  end;
  for i:=0 to N-1 do
    pan[i].Caption:=InttoStr(num[i]);
  i:=0;
  j:=0;
  flag:=true;
  lock:=0;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
   lock:=0;
  GetEmColoured;
  if (num[j]>num[j+1]) then
  begin
    flag:=false;
    while (lock=0) do
    begin
      timer1.Enabled:=false;
      timer2.Enabled:=true;
      break;
    end;
  end;
  j:=j+1;
  if (j=N-i-1) then
  begin
    j:=0;
    i:=i+1;
    if (flag=true) then
    begin
      timer1.Enabled:=false;
      for i:=0 to N-1 do
        pan[i].Color:=clBlue;
    end;
    if (i=N-1) then
      timer1.Enabled:=false;
  end;
  flag:=true;
  lock:=0;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  if (j=0) and (i<>0) and (i<>N) then
  begin
    ChangeEmFinally;
  end else begin
    ChangeEm;
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  Timer1.Enabled:=True;
  flag:=true;
  i:=0;
  for i:=0 to N-1 do
  begin
   num[i]:=smth[i];
   pan[i].Caption:=InttoStr(num[i]);
   pan[i].Color:=clBtnFace;
  end;
  i:=0;
  j:=0;
end;

procedure TForm1.GetEmColoured;
begin
   if (i<>0) then
    for t:=N-1 downto N-i-1 do
      pan[t].Color:=clBlue;
  for t:=0 to N-i-1 do
    pan[t].Color:=clBtnFace;
  if (j<>0) then
    pan[j-1].Color:=clBtnFace;
  pan[j].Color:=clRed;
  pan[j+1].Color:=clRed;
end;

procedure TForm1.ChangeEm;
begin
    pan[j-1].Left:=pan[j-1].Left+V;
  pan[j].Left:=pan[j].Left-V;
  if (pan[j].Left=170+(A+L)*(j-1)) then
  begin
    pan[j-1].Left:=170+(A+L)*(j-1);
    pan[j].Left:=170+(A+L)*j;
    t:=num[j];
    num[j]:=num[j-1];
    num[j-1]:=t;
    pan[j-1].Caption:=inttostr(num[j-1]);
    pan[j].Caption:=inttostr(num[j]);
    lock:=1;
    timer1.Enabled:=true;
    timer2.Enabled:=false;
  end;
end;

procedure TForm1.ChangeEmFinally;
begin
   pan[N-i-1].Left:=pan[N-i-1].Left+V;
  pan[N-i].Left:=pan[N-i].Left-V;
  if (pan[N-i].Left=170+(A+L)*(N-i-1)) then
  begin
    pan[N-i-1].Left:=170+(A+L)*(N-i-1);
    pan[N-i].Left:=170+(A+L)*(N-i);
    t:=num[N-i];
    num[N-i]:=num[N-i-1];
    num[N-i-1]:=t;
    pan[N-i-1].Caption:=inttostr(num[N-i-1]);
    pan[N-i].Caption:=inttostr(num[N-i]);
    lock:=1;
    if (i<>N-1) then
    timer1.Enabled:=true;
    timer2.Enabled:=false;
  end;
end;

end.
